define(["moment"], function (moment) {
    return ["$state", "$scope", "$rootScope", "$filter", "$stateParams", "config", "MMWService", "$mdDialog", "$timeout", "$calculater", 
        function ($state, $scope, $rootScope, $filter, $stateParams, config, $MMWService, $mdDialog, $timeout, $calculater) {
            $scope.init = function () {
                $scope.new_reel_actual_qty = 0;//新捲料數
                $scope.split_reason = '';//拆分原因
                $scope.org_reel_actual_qty = '';//原物料數量
                $scope.new_reel_no = '';//新物料編
                $scope.org_reel_no = '';//原物料批
                $scope.reel_list = [];
            };

            $scope.open = function(){
                if($scope.new_reel_actual_qty && (+$scope.new_reel_actual_qty) != 0){
                    $scope.org_reel_actual_qty = $scope.org_reel_actual_qty - $scope.new_reel_actual_qty;
                    let openItem = {
                        org_reel_no:$scope.org_reel_no,
                        org_reel_actual_qty:$scope.org_reel_actual_qty,
                        new_reel_no:$scope.new_reel_no,
                        new_reel_actual_qty:$scope.new_reel_actual_qty,
                        split_reason:$scope.split_reason,
                    }
                    $scope.reel_list.unshift(openItem);
                    $scope.new_reel_no='';
                    $scope.new_reel_actual_qty='';
                    $scope.split_reason='';
                }else{
                    $scope.$root.showAlert($filter('translate')('SMT2502.msg.no_sep_qty'))
                }
            }
            
            // QRCode輸入
            $scope.QRCodeScanner = function(){
                $scope.$root.OpenScanner({
                    after : function(result){
                        $scope.new_reel_no = result.text;
                    },
                    exception: function(){
                        //目前裝置無法使用相機
                        $scope.$root.showAlert($filter('translate')('common.msg.cant_use_camera'));
                    },
                    orientation : 'portrait'
                });
            };
            $scope.loadOriginReelNo = function(){
                $rootScope.showQueryList({
                    pageRecord: 50,
                    header: [
                        { label: $filter('translate')('SMT2502.list.origin_reel_no'), code: 'org_reel_no' },
                        { label: $filter('translate')('SMT2502.list.qty'), code: 'org_reel_actual_qty' }
                    ],
                    query: function (conditions, feedback) {
                        $MMWService.sendToServer({
                            uri: 'ServicesSMT.Module_SMT.reel_split_info_get',
                            content: {},
                            success: function (data) {
                                feedback(data.reel_list);
                            }
                        });
                    },
                    selectCode: $scope.org_reel_actual_qty,
                    confirm: function (selectItem, dialog) {
                        $scope.org_reel_no = selectItem.org_reel_no;
                        $scope.org_reel_actual_qty = selectItem.org_reel_actual_qty;
                        dialog.hide();
                    }
                });
            }

            $scope.ChangeQty = function(target, dialog){
                angular.element(target).addClass('is-focus');
                $calculater.open(function(num, calcDialog){
                    angular.element(target).removeClass('is-focus');
                    if($scope.org_reel_actual_qty >= (+num)){
                        calcDialog.hide();
                        $scope.new_reel_actual_qty = (+num);
                    }else{
                        $scope.$root.showAlert($filter('translate')('SMT2502.msg.qty_err'))
                    }           
                },
                function(calcDialog){
                    angular.element(target).removeClass('is-focus');
                    calcDialog.hide();
                },
                {
                    title : $filter('translate')('F0102.calculater-title'),
                    defaultNum : $scope.new_reel_actual_qty,
                    decimal : true
                });
            }

            $scope.back = function () {
                $scope.$root.hideMenu();
                $scope.$root.changeProgram('home', true);
            };

            $scope.confirm = function () {
                let content={
                    reel_list:$scope.reel_list
                }
                $MMWService.sendToServer({
                    uri: 'ServicesSMT.Module_SMT.reel_split_process',
                    content: content,
                    success: function (data, response) {
                        $scope.reel_list=[];
                        $rootScope.showToast($filter('translate')('common.msg.success'));
                    }
                });
            };
            
            $scope.deleteItem = function(key,item){
                $scope.$root.showConfirm($filter('translate')('SMT2502.msg.sure_to_delete'),function(dialog){
                    $scope.reel_list = $scope.reel_list.filter(function(item_info){
                        return item_info.org_reel_no != item.org_reel_no;
                    })
                });
            }
        }
    ];
});
